<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
$cashierid=$this->checklogin();
$return['steps']=array(
	array('title'=>'选择时间','en_title'=>'basic info'),
	array('title'=>'结算消息','en_title'=>'Shareholders'),
	array('title'=>'创建结算单','en_title'=>'Team info'),
);
if($_RQ['op']=='edit'){
	$return['step']=0;
	$postdata=get_postdata();
	if($postdata){
		if(!$postdata['year']){
			exi('请选择年份','error');
		}
		if(!$postdata['month']){
			exi('请选择月份','error');
		} 
		if($postdata['month']<10){
			$postdata['month']='0'.$postdata['month'];
		}
		if(!$postdata['week']){
			$firstday = strtotime($postdata['year'] .$postdata['month'] . "01000000");
			$firstdaystr = date("Y-m-01", $firstday);
			$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
		}else{
			if($postdata['week']<4){
				$day=($postdata['week']-1)*7+1;		
				if($day<10){
					$day='0'.$day;
				}
				$firstday = strtotime($postdata['year'] .$postdata['month'] .$day. "000000");
				$firstdaystr = date("Y-m-d", $firstday);
				$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr  +6 day")));
			}else{
				$firstday = strtotime($postdata['year'] .$postdata['month'] . "22000000");
				$firstdaystr = date("Y-m-01", $firstday);
				$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
			}
		}
		$pay_log=pdo_getall('jy_cashier_pay_log',array('createtime between'=>array($firstday,$lastday),'cashierid'=>$cashierid,'paytype'=>0,'status'=>1));
		$salsecount=count($pay_log);
		if($pay_log){
			$user=array();
			$item['ordermoney']=0;
			$item['commission_money']=0;
			$sys_choufee=0;
			foreach($pay_log as $k=>$v){
				$item['ordermoney'] +=$v['money'];
				$item['commission_money'] +=$commission_money;
			}
		}		
		if(!$salsecount){
			exi('没有可结算订单','error');
		}
		$item['starttime']=$firstday;
		$item['endtime']=$lastday;
		$item['salsecount']=$salsecount;
		$item['realprice']=$item['ordermoney']?$item['ordermoney']:0;	
		$item['finalprice']=$item['realprice']<=0?0:$item['realprice'];
		$this->session('item',$item);
		exi('','','step1');
	}
	$year=intval(date('Y',time()));
	$item['year']=''.$year.'';
	$return['year']=array('0'=>array('id'=>$year-9,'title'=>$year-9),'1'=>array('id'=>$year-8,'title'=>$year-8),'2'=>array('id'=>$year-7,'title'=>$year-7),'3'=>array('id'=>$year-6,'title'=>$year-6),'4'=>array('id'=>$year-5,'title'=>$year-5),'5'=>array('id'=>$year-4,'title'=>$year-4),'6'=>array('id'=>$year-3,'title'=>$year-3),'7'=>array('id'=>$year-2,'title'=>$year-2),'8'=>array('id'=>$year-1,'title'=>$year-1),'9'=>array('id'=>$year,'title'=>$year));
	$return['month']=array('0'=>array('id'=>1,'title'=>1),'1'=>array('id'=>2,'title'=>2),'2'=>array('id'=>3,'title'=>3),'3'=>array('id'=>4,'title'=>4),'4'=>array('id'=>5,'title'=>5),'5'=>array('id'=>6,'title'=>6),'6'=>array('id'=>7,'title'=>7),'7'=>array('id'=>8,'title'=>8),'8'=>array('id'=>9,'title'=>9),'9'=>array('id'=>10,'title'=>10),'10'=>array('id'=>11,'title'=>11),'11'=>array('id'=>12,'title'=>12));
	$return['week']=array('0'=>array('id'=>1,'title'=>'第一周'),'2'=>array('id'=>2,'title'=>'第二周'),'2'=>array('id'=>3,'title'=>'第三周'),'3'=>array('id'=>4,'title'=>'第四周'));
	$return['item']=$item;
	$htmls=array(
			'type'=>'edit',
			'edit'=>array(
				array('type'=>'tip','title'=>'<red>只有微信支付的线下订单才会被结算！</red>'),
				array('name'=>'steps','type'=>'steps'),
				array('name'=>'year','title'=>'选择年份','type'=>'select','options'=>'year'),
				array('name'=>'month','title'=>'选择月份','type'=>'select','options'=>'month'),
			)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='step1'){
	$return['step']=1;
	$return['pay_types']=$this->pay_types;
	$item=$this->session('item');
	$item['ratefee']=price($item['ratefee']);
	$item['sys_choufee']=price($item['sys_choufee']);
	$item['finalprice']=price($item['finalprice']);
	$item['pay_type']='wechat';
	$return['item']=$item;
	$htmls=array(
				'type'=>'edit',
				'post'=>'step2',
				'edit'=>array(
					array('name'=>'pay_type','title'=>'打款方式','type'=>'radio','options'=>'pay_types'),
					array('name'=>'salsecount','title'=>'商品销量','type'=>'unitstring','unit'=>'个','read'=>true),
					array('name'=>'ordermoney','title'=>'订单金额','type'=>'unitstring','unit'=>'元','read'=>true),
					array('name'=>'realprice','title'=>'提现金额','type'=>'unitstring','unit'=>'元','read'=>true),
					array('name'=>'feerate','title'=>'提现费率','type'=>'unitstring','read'=>true,'unit'=>'点','help'=>'手续费百分比'),
					array('name'=>'ratefee','title'=>'提现手续费','type'=>'unitstring','unit'=>'元','read'=>true),
					array('name'=>'finalprice','title'=>'到账金额','type'=>'unitstring','unit'=>'元','read'=>true),
					array('type'=>'text','title'=>'申请事由','name'=>'content'),
				)
	);
	$return['htmls']=$htmls;
	exi($return);
	
}
if($_RQ['op']=='step2'){
	$return['step']=2;
	$postdata=get_postdata();
	$data=array(
		'clearno'=>get_ordersn('jy_cashier_clearing','clearno'),
		'realmoney'=>$postdata['realprice'],
		'money'=>$postdata['ordermoney'],
		'charge'=>$postdata['charge'],
		'paytype'=>0,
		'status'=>0,
	);
	$data['uniacid']=$_SESSION['uniacid'];
	$data['cashierid']=$cashierid;
	$data['createtime']=time();
	pdo_insert('jy_cashier_clearing',$data);
	pdo_update('jy_cashier_pay_log',array('status'=>2),array('createtime between'=>array($postdata['starttime'],$postdata['endtime']),'cashierid'=>$cashierid,'paytype'=>0,'status'=>1));
	exi('创建结算单成功','','/manage.clearing/');
}